﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WROA.Models.Services;
using DevExpress.Xpo;
using WROA.Models.ViewModels;
using WROA.Models.Entitys;
using WROA.Models.ViewModels.ApproveModels;
using WROA.Models.IServices;

namespace WROA.Controllers
{
    /// <summary>
    /// 审批Controller
    /// </summary>
    public class ApproveController : Controller
    {
        /// <summary>
        /// 查询待审批的所有公文申请
        /// </summary>
        /// <returns></returns>
        public ActionResult AllDocument()
        {
            IUserService us = new UserService();
            IDocumentService ds = new DocumentService();
            string position = us.SelectPosition(User.Identity.Name);//根据用户名查询职位
            XPCollection allApplyDocument = ds.GetAllApplyDocument(User.Identity.Name, position);//获取待审批的公文

            AllDocumentView model = new AllDocumentView(allApplyDocument);
            return View(model);
        }

        /// <summary>
        /// 根据id查询待审批公文申请
        /// </summary>
        /// <returns></returns>
        public ActionResult Document()
        {
            int id = (int)RouteData.Values["id"];//从url获取id
            IDocumentService ds = new DocumentService();
            ApplyDocument applyDocument = ds.GetApplyDocument(id);//获取公文申请
            XPCollection documentApproves = applyDocument.documentapproves;//获取公文审批

            DocumentView model = new DocumentView(applyDocument, documentApproves);
            return View(model);
        }

        /// <summary>
        /// 公文审批批准
        /// </summary>
        /// <returns></returns>
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult DocumentAgree(FormCollection form)
        {
            IDocumentService ds = new DocumentService();
            IUserService us = new UserService();
            IFlowService fs = new FlowService();
            IMessageService msg = new MessageService();

            int id = Convert.ToInt32(form["id"]);//从隐藏字段获取公文申请id
            ApplyDocument applyDocument = ds.GetApplyDocument(id);//获取公文申请对象

            //保存审批内容
            DocumentApprove documentApprove = new DocumentApprove();
            documentApprove.Applydocument = applyDocument;
            documentApprove.User = us.GetUser(User.Identity.Name);
            documentApprove.Da_content = form["Content"];
            documentApprove.Da_date = DateTime.Now;
            documentApprove.Da_result = "批准";
            ds.Save(documentApprove);

            if (fs.ProcedureIsLastStep(applyDocument.Procedure.Oid) == true)//如果是流程的最后一个步骤
            {
                ds.UpdateApplyDocumentState(id);//更新公文申请流程为已经结束
                ds.UpdateApplyDocumentIsAgree(id);//更新公文申请流程为已经批准
                //通知该通知的人,短信，邮件
                //......
                msg.SendMessage(User.Identity.Name, applyDocument.User.U_name, "");//发送邮件给申请者批准信息
            }
            else//如果不是最后一个步骤，则进入下一个步骤
            {
                //获取下一步步骤
                applyDocument.Procedure = ds.GetProcedure(applyDocument.Flow, applyDocument.Procedure.Step_id + 1);
                ds.Save(applyDocument);
            }

            return RedirectToAction("Index","Home");
        }

        /// <summary>
        /// 公文审批不批准
        /// </summary>
        /// <param name="form"></param>
        /// <returns></returns>
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult DocumentDisagree(FormCollection form)
        {
            IDocumentService ds = new DocumentService();
            IUserService us = new UserService();
            IFlowService fs = new FlowService();
            IMessageService msg = new MessageService();

            int id = Convert.ToInt32(form["id"]);//从隐藏字段获取公文申请id
            ApplyDocument applyDocument = ds.GetApplyDocument(id);//获取公文申请对象

            //保存审批内容
            DocumentApprove documentApprove = new DocumentApprove();
            documentApprove.Applydocument = applyDocument;
            documentApprove.User = us.GetUser(User.Identity.Name);
            documentApprove.Da_content = form["Content"];
            documentApprove.Da_date = DateTime.Now;
            documentApprove.Da_result = "不批准";
            ds.SaveDocumentApprove(documentApprove);//保存公文审批信息,修改公文申请表状态为不批准和结束

            //通知该通知的人,短信，邮件
            msg.SendMessage(User.Identity.Name, applyDocument.User.U_name, "");//发送邮件给申请者不批准信息

            return RedirectToAction("Index", "Home");
        }

        /// <summary>
        /// 查询待审批的所有会议申请
        /// </summary>
        /// <returns></returns>
        public ActionResult AllMeeting()
        {
            IUserService us = new UserService();
            IMeetingService ms = new MeetingService();
            string position = us.SelectPosition(User.Identity.Name);//根据用户名查询职位
            XPCollection allApplyMeeting = ms.GetAllApplyMeeting(User.Identity.Name, position);//获取待审批的公文

            AllMeetingView model = new AllMeetingView(allApplyMeeting);
            return View(model);
        }

        /// <summary>
        /// 根据id查询待审批会议申请
        /// </summary>
        /// <returns></returns>
        public ActionResult Meeting()
        {
            int id = (int)RouteData.Values["id"];//从url获取id
            IMeetingService ms = new MeetingService();
            ApplyMeeting applyMeeting = ms.GetApplyMeeting(id);//获取公文申请
            XPCollection meetingApproves = applyMeeting.meetingapproves;//获取公文审批

            MeetingView model = new MeetingView(applyMeeting, meetingApproves);
            return View(model);
        }

        /// <summary>
        /// 会议审批批准
        /// </summary>
        /// <returns></returns>
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult MeetingAgree(FormCollection form)
        {
            IMeetingService ms = new MeetingService();
            IUserService us = new UserService();
            IFlowService fs = new FlowService();
            IMessageService msg = new MessageService();

            int id = Convert.ToInt32(form["id"]);//从隐藏字段获取公文申请id
            ApplyMeeting applyMeeting = ms.GetApplyMeeting(id);//获取会议申请对象

            //保存审批内容
            MeetingApprove meetingApprove = new MeetingApprove();
            meetingApprove.Applymeeting = applyMeeting;
            meetingApprove.User = us.GetUser(User.Identity.Name);
            meetingApprove.Ma_content = form["Content"];
            meetingApprove.Ma_date = DateTime.Now;
            meetingApprove.Ma_result = "批准";
            ms.Save(meetingApprove);

            if (fs.ProcedureIsLastStep(applyMeeting.Procedure.Oid) == true)//如果是流程的最后一个步骤
            {
                ms.UpdateApplyMeetingState(id);//更新会议申请流程为已经结束
                ms.UpdateApplyMeetingIsAgree(id);//更新会议申请流程为已经批准
                //通知该通知的人,短信，邮件
                //......
                msg.SendMessage(User.Identity.Name, applyMeeting.User.U_name, "");//发送邮件给申请者批准信息
                List<string> receivers = new List<string>();//获取乘车人员姓名列表
                foreach (MeetingUser mu in applyMeeting.Meetingdata.meetingusers)
                {
                    receivers.Add(mu.UserData.U_name);
                }
                msg.MassTexting(User.Identity.Name, receivers, "");//发送邮件
            }
            else//如果不是最后一个步骤，则进入下一个步骤
            {
                //获取下一步步骤
                applyMeeting.Procedure = ms.GetProcedure(applyMeeting.Flow, applyMeeting.Procedure.Step_id + 1);
                ms.Save(applyMeeting);
            }

            return RedirectToAction("Index", "Home");
        }


        /// <summary>
        /// 会议审批不批准
        /// </summary>
        /// <param name="form"></param>
        /// <returns></returns>
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult MeetingDisagree(FormCollection form)
        {
            IMeetingService ms = new MeetingService();
            IUserService us = new UserService();
            IFlowService fs = new FlowService();
            IMessageService msg = new MessageService();

            int id = Convert.ToInt32(form["id"]);//从隐藏字段获取公文申请id
            ApplyMeeting applyMeeting = ms.GetApplyMeeting(id);//获取公文申请对象

            //保存审批内容
            MeetingApprove meetingApprove = new MeetingApprove();
            meetingApprove.Applymeeting = applyMeeting;
            meetingApprove.User = us.GetUser(User.Identity.Name);
            meetingApprove.Ma_content = form["Content"];
            meetingApprove.Ma_date = DateTime.Now;
            meetingApprove.Ma_result = "不批准";
            ms.SaveMeetingApprove(meetingApprove);//保存公文审批信息,修改公文申请表状态为不批准和结束

            //通知该通知的人,短信，邮件
            msg.SendMessage(User.Identity.Name, applyMeeting.User.U_name, "");//发送邮件给申请者不批准信息
            
            //删除其他表信息


            return RedirectToAction("Index", "Home");
        }

        /// <summary>
        /// 查询待审批的所有用车申请
        /// </summary>
        /// <returns></returns>
        public ActionResult AllCar()
        {
            IUserService us = new UserService();
            ICarService cs = new CarService();
            string position = us.SelectPosition(User.Identity.Name);//根据用户名查询职位
            XPCollection allApplyCar = cs.GetAllApplyCar(User.Identity.Name, position);//获取待审批的用车申请

            AllCarView model = new AllCarView(allApplyCar);
            return View(model);
        }

        /// <summary>
        /// 根据id查询待审批用车申请
        /// </summary>
        /// <returns></returns>
        public ActionResult Car()
        {
            int id = (int)RouteData.Values["id"];//从url获取id
            IDocumentService ds = new DocumentService();
            ApplyDocument applyDocument = ds.GetApplyDocument(id);//获取公文申请
            XPCollection documentApproves = applyDocument.documentapproves;//获取公文审批

            DocumentView model = new DocumentView(applyDocument, documentApproves);
            return View(model);
        }

        /// <summary>
        /// 用车审批批准
        /// </summary>
        /// <returns></returns>
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult CarAgree(FormCollection form)
        {
            ICarService cs = new CarService();
            IUserService us = new UserService();
            IFlowService fs = new FlowService();
            IDocumentService ds = new DocumentService();
            IMessageService msg = new MessageService();

            int id = Convert.ToInt32(form["id"]);//从隐藏字段获取用车申请id
            ApplyCar applyCar = cs.GetApplyCar(id);//获取用车申请对象

            //保存审批内容
            CarApprove carApprove = new CarApprove();
            carApprove.Applycar = applyCar;
            carApprove.User = us.GetUser(User.Identity.Name);
            carApprove.Ca_content = form["Content"];
            carApprove.Ca_date = DateTime.Now;
            carApprove.Ca_result = "批准";
            cs.Save(carApprove);

            if (fs.ProcedureIsLastStep(applyCar.Procedure.Oid) == true)//如果是流程的最后一个步骤
            {
                cs.UpdateApplyCarState(id);//更新用车申请流程为已经结束
                cs.UpdateApplyCarIsAgree(id);//更新用车申请流程为已经批准

                //通知该通知的人,短信，邮件
                //......

                msg.SendMessage(User.Identity.Name, applyCar.User.U_name, "");//发送邮件给申请者批准信息
                List<string> receivers = new List<string>();//获取乘车人员姓名列表
                foreach (Passenger p in applyCar.Applycardata.passengers)
                {
                    receivers.Add(p.User.U_name);
                }
                msg.MassTexting(User.Identity.Name, receivers, "");//发送邮件

            }
            else//如果不是最后一个步骤，则进入下一个步骤
            {
                //获取下一步步骤
                applyCar.Procedure = ds.GetProcedure(applyCar.Flow, applyCar.Procedure.Step_id + 1);
                ds.Save(applyCar);
            }

            return RedirectToAction("Index", "Home");
        }

        /// <summary>
        /// 用车审批不批准
        /// </summary>
        /// <param name="form"></param>
        /// <returns></returns>
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult CarDisagree(FormCollection form)
        {
            ICarService cs = new CarService();
            IUserService us = new UserService();
            IFlowService fs = new FlowService();
            IMessageService msg = new MessageService();

            int id = Convert.ToInt32(form["id"]);//从隐藏字段获取公文申请id
            ApplyCar applyCar = cs.GetApplyCar(id);//获取公文申请对象

            //保存审批内容
            CarApprove carApprove = new CarApprove();
            carApprove.Applycar = applyCar;
            carApprove.User = us.GetUser(User.Identity.Name);
            carApprove.Ca_content = form["Content"];
            carApprove.Ca_date = DateTime.Now;
            carApprove.Ca_result = "不批准";
            cs.SaveCarApprove(carApprove);//保存公文审批信息,修改公文申请表状态为不批准和结束

            //通知该通知的人,短信，邮件
            //......
            msg.SendMessage(User.Identity.Name, applyCar.User.U_name, "");//发送邮件给申请者

            //删除其他表信息
            //applyCar.Applycardata.passengers.

            return RedirectToAction("Index", "Home");
        }
    }
}
